package com.cybertron.ironhide.search.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cybertron.ironhide.search.common.base.Pager;
import com.cybertron.ironhide.search.domain.Comment;
import com.cybertron.ironhide.search.service.CommentService;

@Controller
@RequestMapping("/comment")
public class CommentController {

	@Autowired(required = false)
	private CommentService commentService;

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String get(@PathVariable("id") String id, Model model, Pager pager) {
		List<Comment> comments = commentService.selectByArticleId(id, pager);
		model.addAttribute("comments", comments);
		model.addAttribute("id", id);
		return "comment";
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.POST)
	@ResponseBody
	public Comment add(@PathVariable("id") String id, Comment comment) {
		comment.setArticleId(id);
		commentService.insertSelective(comment);
		return comment;
	}
}
